
analysis_df_subset <-
  analysis_df_subset %>%
    mutate(q17_survey_java1 = case_when(q17_survey_java1 == "Sangat setuju" ~ 4,
                                        q17_survey_java1 == "Cukup setuju" ~ 3,
                                        q17_survey_java1 == "Tidak setuju" ~ 2,
                                        q17_survey_java1 == "Sangat tidak setuju" ~ 1,
                                        TRUE ~ NA_real_),
           q17_survey_java2 = case_when(q17_survey_java2 == "Sangat setuju" ~ 4,
                                        q17_survey_java2 == "Cukup setuju" ~ 3,
                                        q17_survey_java2 == "Tidak setuju" ~ 2,
                                        q17_survey_java2 == "Sangat tidak setuju" ~ 1,
                                        TRUE ~ NA_real_),
           q17_survey_java3 = case_when(q17_survey_java3 == "Sangat setuju" ~ 4,
                                        q17_survey_java3 == "Cukup setuju" ~ 3,
                                        q17_survey_java3 == "Tidak setuju" ~ 2,
                                        q17_survey_java3 == "Sangat tidak setuju" ~ 1,
                                        TRUE ~ NA_real_),
           
           q18_survey_daerah1 = case_when(q18_survey_daerah1 == "Sangat setuju" ~ 4,
                                          q18_survey_daerah1 == "Cukup setuju" ~ 3,
                                          q18_survey_daerah1 == "Tidak setuju" ~ 2,
                                          q18_survey_daerah1 == "Sangat tidak setuju" ~ 1,
                                          TRUE ~ NA_real_),
           q18_survey_daerah2 = case_when(q18_survey_daerah2 == "Sangat setuju" ~ 4,
                                          q18_survey_daerah2 == "Cukup setuju" ~ 3,
                                          q18_survey_daerah2 == "Tidak setuju" ~ 2,
                                          q18_survey_daerah2 == "Sangat tidak setuju" ~ 1,
                                          TRUE ~ NA_real_),
           q18_survey_daerah3 = case_when(q18_survey_daerah3 == "Sangat setuju" ~ 4,
                                          q18_survey_daerah3 == "Cukup setuju" ~ 3,
                                          q18_survey_daerah3 == "Tidak setuju" ~ 2,
                                          q18_survey_daerah3 == "Sangat tidak setuju" ~ 1,
                                          TRUE ~ NA_real_),
           q19_survey_relg1 = case_when(q19_survey_relg1 == "Sangat keberatan" ~ 4,
                                        q19_survey_relg1 == "Keberatan" ~ 3,
                                        q19_survey_relg1 == "Tidak keberatan" ~ 2,
                                        q19_survey_relg1 == "Sangat tidak keberatan" ~ 1,
                                        TRUE ~ NA_real_),
           q19_survey_relg2 = case_when(q19_survey_relg2 == "Sangat keberatan" ~ 4,
                                        q19_survey_relg2 == "Keberatan" ~ 3,
                                        q19_survey_relg2 == "Tidak keberatan" ~ 2,
                                        q19_survey_relg2 == "Sangat tidak keberatan" ~ 1,
                                        TRUE ~ NA_real_),
           q19_survey_relg3 = case_when(q19_survey_relg3 == "Sangat keberatan" ~ 4,
                                        q19_survey_relg3 == "Keberatan" ~ 3,
                                        q19_survey_relg3 == "Tidak keberatan" ~ 2,
                                        q19_survey_relg3 == "Sangat tidak keberatan" ~ 1,
                                        TRUE ~ NA_real_),
           q13_survey_success_reason_1_merit_h2 = case_when(q13_survey_success_reason_1_merit == "Sangat tidak penting" ~ 4,
                                                            q13_survey_success_reason_1_merit == "Kurang penting" ~ 3,
                                                            q13_survey_success_reason_1_merit == "Cukup penting" ~ 2,
                                                            q13_survey_success_reason_1_merit == "Sangat penting" ~ 1,
                                                            TRUE ~ NA_real_),
           q13_survey_success_reason_2_connection_h2 = case_when(q13_survey_success_reason_2_connection == "Sangat penting" ~ 4,
                                                                 q13_survey_success_reason_2_connection == "Cukup penting" ~ 3,
                                                                 q13_survey_success_reason_2_connection == "Kurang penting" ~ 2,
                                                                 q13_survey_success_reason_2_connection == "Sangat tidak penting" ~ 1,
                                                                 TRUE ~ NA_real_),
           q13_survey_success_reason_3_sara = case_when(q13_survey_success_reason_3_sara == "Sangat penting" ~ 4,
                                                        q13_survey_success_reason_3_sara == "Cukup penting" ~ 3,
                                                        q13_survey_success_reason_3_sara == "Kurang penting" ~ 2,
                                                        q13_survey_success_reason_3_sara == "Sangat tidak penting" ~ 1,
                                                        TRUE ~ NA_real_),
           q14_survey_test_vs_connection = case_when(q14_survey_test_vs_connection == "Koneksi" ~ 1,
                                                     q14_survey_test_vs_connection == "Tes" ~ 0,
                                                     TRUE ~ NA_real_),
           q15_survey_test_muslim = case_when(q15_survey_test_muslim == "Sangat setuju" ~ 4,
                                              q15_survey_test_muslim == "Cukup setuju" ~ 3,
                                              q15_survey_test_muslim == "Kurang setuju" ~ 2,
                                              q15_survey_test_muslim == "Sangat tidak setuju" ~ 1,
                                              TRUE ~ NA_real_),
           q15_survey_test_java = case_when(q15_survey_test_java == "Sangat setuju" ~ 4,
                                            q15_survey_test_java == "Cukup setuju" ~ 3,
                                            q15_survey_test_java == "Kurang setuju" ~ 2,
                                            q15_survey_test_java == "Sangat tidak setuju" ~ 1,
                                            TRUE ~ NA_real_),
           q16_survey_transparent_h2 = case_when(q16_survey_transparent == "Sangat tidak transparan" ~ 4,
                                                 q16_survey_transparent == "Kurang transparan" ~ 3,
                                                 q16_survey_transparent == "Cukup transparan" ~ 2,
                                                 q16_survey_transparent == "Sangat transparan" ~ 1),
           q20_survey_pancasila_h3 = case_when(q20_survey_pancasila_h3 == "Sangat tidak relevan" ~ 1,
                                               q20_survey_pancasila_h3 == "Kurang relevan" ~ 2,
                                               q20_survey_pancasila_h3 == "Cukup relevan" ~ 3,
                                               q20_survey_pancasila_h3 == "Sangat relevan" ~ 4,
                                               TRUE ~ NA_real_),
           q21_survey_nation_ethnic_h3 = case_when(q21_survey_nation_ethnic_h3 == "Suku-bangsa" ~ 1,
                                                   q21_survey_nation_ethnic_h3 == "Saya merasa saya adalah bagian dari keduanya" ~ 2,
                                                   q21_survey_nation_ethnic_h3 == "Orang Indonesia" ~ 3,
                                                   TRUE ~ NA_real_),
           q21_survey_nation_ethnic_h3_binary = case_when(q21_survey_nation_ethnic_h3 %in% c(1, 2) ~ 0, 
                                                          q21_survey_nation_ethnic_h3 == 3 ~ 1,
                                                          TRUE ~ NA_real_)
           
    ) %>%
      
      #income and career satisfaction
      mutate(
        q26_survey_income_recode = case_when(q26_survey_income == "Di bawah 1 juta" ~ 1,
                                             q26_survey_income == "1-2 juta" ~ 2,
                                             q26_survey_income == "2-3 juta" ~ 3,
                                             q26_survey_income == "3-4 juta" ~ 4,
                                             q26_survey_income == "4-5 juta" ~ 5,
                                             q26_survey_income == "5-6 juta" ~ 6,
                                             q26_survey_income == "6-7 juta" ~ 7,
                                             q26_survey_income == "Lebih dari 7 juta" ~ 8,
                                             TRUE ~ NA_real_),
        q27_survey_job_satis = case_when(q27_survey_job_satis == "Sangat tidak puas" ~ 1,
                                         q27_survey_job_satis == "Kurang puas" ~ 2,
                                         q27_survey_job_satis == "Cukup puas" ~ 3,
                                         q27_survey_job_satis == "Sangat puas" ~ 4,
                                         TRUE ~ NA_real_)
      )




##making indices

#----------------------------------------------------------------------------------
##kling indices for passing final exam --------------------------------------------
#----------------------------------------------------------------------------------

#make various subsets ----------------------------------------------------------
estimation_sample_javanese = analysis_df_subset %>% filter(abs(forcing_final) < 1, java_indicator == 1)
estimation_sample_non_javanese = analysis_df_subset %>% filter(abs(forcing_final) < 1, java_indicator == 0)
estimation_sample = analysis_df_subset %>% filter(abs(forcing_final) < 1)

#javanese preferntialism ----------------------------------------------------------
estimation_sample_javanese$java_index_final_bw_1 = kling_index(.data = estimation_sample_javanese,
                                                         .impute = F,
                                                         ... = q17_survey_java1, q17_survey_java2)

estimation_sample_non_javanese$non_java_index_final_bw_1 = kling_index(.data = estimation_sample_non_javanese,
                                                                 .impute = F,
                                                                 ... = q17_survey_java1, q17_survey_java2)

#regional preferntialism ----------------------------------------------------------
estimation_sample$regional_index_final_bw_1 =  kling_index(.data = estimation_sample,
                                                     .impute = F,
                                                     ... = q18_survey_daerah1, q18_survey_daerah2, q18_survey_daerah3)

#religious intolerance ----------------------------------------------------------
estimation_sample$religious_index_final_bw_1 =  kling_index(.data = estimation_sample,
                                                      .impute = F,
                                                      ... = q19_survey_relg1, q19_survey_relg2, q19_survey_relg3)

#nationalism---------------------------------------------------------------------
estimation_sample$national_index_final_bw_1  =  kling_index(.data = estimation_sample,
                                                      .impute = F,
                                                      ... = q20_survey_pancasila_h3, q21_survey_nation_ethnic_h3)

#corruption----------------------------------------------------------------------
estimation_sample$corruption_index_final_bw_1  = kling_index(.data = estimation_sample,
                                                       .impute = F,
                                                       ... = q16_survey_transparent_h2, q13_survey_success_reason_1_merit_h2, 
                                                       q13_survey_success_reason_3_sara, q13_survey_success_reason_2_connection_h2,
                                                       q14_survey_test_vs_connection)


#merge in
analysis_df_subset %<>%
  left_join(., estimation_sample_javanese %>% dplyr::select(ROWNUM_ID, java_index_final_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample_non_javanese %>% dplyr::select(ROWNUM_ID, non_java_index_final_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, regional_index_final_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, religious_index_final_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, national_index_final_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, corruption_index_final_bw_1), by = "ROWNUM_ID")



#----------------------------------------------------------------------------------
##kling indices for point estimates (1pp) for SKD treatment--------------------------
#----------------------------------------------------------------------------------
#N.B. this particular exam is out of 500 points, which is why subsetting to 5 points is 1pp.

#make various subsets ----------------------------------------------------------
estimation_sample_javanese = analysis_df_subset %>% filter(abs(forcing_skd) < 5, java_indicator == 1)
estimation_sample_non_javanese = analysis_df_subset %>% filter(abs(forcing_skd) < 5, java_indicator == 0)
estimation_sample = analysis_df_subset %>% filter(abs(forcing_skd) < 5)


#javanese preferntialism ----------------------------------------------------------
estimation_sample_javanese$java_index_skd_bw_1 = kling_index_f3(.data = estimation_sample_javanese,
                                                            .impute = F,
                                                            ... = q17_survey_java1, q17_survey_java2)

estimation_sample_non_javanese$non_java_index_skd_bw_1 = kling_index_f3(.data = estimation_sample_non_javanese,
                                                                    .impute = F,
                                                                    ... = q17_survey_java1, q17_survey_java2,)

#regional preferntialism ----------------------------------------------------------
estimation_sample$regional_index_skd_bw_1 =  kling_index_f3(.data = estimation_sample,
                                                        .impute = F,
                                                        ... = q18_survey_daerah1, q18_survey_daerah2, q18_survey_daerah3)

#religious intolerance ----------------------------------------------------------
estimation_sample$religious_index_skd_bw_1 =  kling_index_f3(.data = estimation_sample,
                                                         .impute = F,
                                                         ... = q19_survey_relg1, q19_survey_relg2, q19_survey_relg3)

#nationalism---------------------------------------------------------------------
estimation_sample$national_index_skd_bw_1  =  kling_index_f3(.data = estimation_sample,
                                                         .impute = F,
                                                         ... = q20_survey_pancasila_h3, q21_survey_nation_ethnic_h3)

#corruption----------------------------------------------------------------------
estimation_sample$corruption_index_skd_bw_1  = kling_index_f3(.data = estimation_sample,
                                                          .impute = F,
                                                          ... = q16_survey_transparent_h2, q13_survey_success_reason_1_merit_h2, 
                                                          q13_survey_success_reason_3_sara, q13_survey_success_reason_2_connection_h2,
                                                          q14_survey_test_vs_connection)

#merge in
analysis_df_subset %<>%
  left_join(., estimation_sample_javanese %>% dplyr::select(ROWNUM_ID, java_index_skd_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample_non_javanese %>% dplyr::select(ROWNUM_ID, non_java_index_skd_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, regional_index_skd_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, religious_index_skd_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, national_index_skd_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, corruption_index_skd_bw_1), by = "ROWNUM_ID")


#----------------------------------------------------------------------------------
##kling indices for matriculant analysis-------------------------------------------
#----------------------------------------------------------------------------------

#make various subsets ----------------------------------------------------------
estimation_sample_javanese = analysis_df_subset %>% filter(abs(forcing_final) < 1, !is.na(matriculant), got_job == 1, java_indicator == 1)
estimation_sample_non_javanese = analysis_df_subset %>% filter(abs(forcing_final) < 1, !is.na(matriculant), got_job == 1, java_indicator == 0)
estimation_sample = analysis_df_subset %>% filter(abs(forcing_final) < 1, !is.na(matriculant), got_job == 1)


#javanese preferntialism ----------------------------------------------------------
estimation_sample_javanese$java_index_matriculant_bw_1 = kling_index_matriculant(.data = estimation_sample_javanese,
                                                                     .impute = F,
                                                                     ... = q17_survey_java1, q17_survey_java2)

estimation_sample_non_javanese$non_java_index_matriculant_bw_1 = kling_index_matriculant(.data = estimation_sample_non_javanese,
                                                                             .impute = F,
                                                                             ... = q17_survey_java1, q17_survey_java2)

#regional preferntialism ----------------------------------------------------------
estimation_sample$regional_index_matriculant_bw_1 =  kling_index_matriculant(.data = estimation_sample,
                                                                 .impute = F,
                                                                 ... = q18_survey_daerah1, q18_survey_daerah2, q18_survey_daerah3)

#religious intolerance ----------------------------------------------------------
estimation_sample$religious_index_matriculant_bw_1 =  kling_index_matriculant(.data = estimation_sample, 
                                                                  .impute = F,
                                                                  ... = q19_survey_relg1, q19_survey_relg2, q19_survey_relg3)

#nationalism---------------------------------------------------------------------
estimation_sample$national_index_matriculant_bw_1  =  kling_index_matriculant(.data = estimation_sample, 
                                                                  .impute = F,
                                                                  ... = q20_survey_pancasila_h3, q21_survey_nation_ethnic_h3)

#corruption----------------------------------------------------------------------
estimation_sample$corruption_index_matriculant_bw_1  = kling_index_matriculant(.data = estimation_sample,
                                                                   .impute = F,
                                                                   ... = q16_survey_transparent_h2, q13_survey_success_reason_1_merit_h2, 
                                                                   q13_survey_success_reason_3_sara, q13_survey_success_reason_2_connection_h2,
                                                                   q14_survey_test_vs_connection)

#merge in
analysis_df_subset %<>%
  left_join(., estimation_sample_javanese %>% dplyr::select(ROWNUM_ID, java_index_matriculant_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample_non_javanese %>% dplyr::select(ROWNUM_ID, non_java_index_matriculant_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, regional_index_matriculant_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, religious_index_matriculant_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, national_index_matriculant_bw_1), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, corruption_index_matriculant_bw_1), by = "ROWNUM_ID")

#----------------------------------------------------------------------------------
##kling indices for point estimates (5pp) for SKD treatment--------------------------
#----------------------------------------------------------------------------------
#N.B. this particular exam is out of 500 points, which is why subsetting to 25 points is 5pp.

#make various subsets ----------------------------------------------------------
estimation_sample_javanese = analysis_df_subset %>% filter(abs(forcing_skd) < 25, java_indicator == 1)
estimation_sample_non_javanese = analysis_df_subset %>% filter(abs(forcing_skd) < 25, java_indicator == 0)
estimation_sample = analysis_df_subset %>% filter(abs(forcing_skd) < 25)


#javanese preferntialism ----------------------------------------------------------
estimation_sample_javanese$java_index_skd_bw_5 = kling_index_f3(.data = estimation_sample_javanese,
                                                                .impute = F,
                                                                ... = q17_survey_java1, q17_survey_java2)

estimation_sample_non_javanese$non_java_index_skd_bw_5 = kling_index_f3(.data = estimation_sample_non_javanese,
                                                                        .impute = F,
                                                                        ... = q17_survey_java1, q17_survey_java2,)

#regional preferntialism ----------------------------------------------------------
estimation_sample$regional_index_skd_bw_5 =  kling_index_f3(.data = estimation_sample,
                                                            .impute = F,
                                                            ... = q18_survey_daerah1, q18_survey_daerah2, q18_survey_daerah3)

#religious intolerance ----------------------------------------------------------
estimation_sample$religious_index_skd_bw_5 =  kling_index_f3(.data = estimation_sample,
                                                             .impute = F,
                                                             ... = q19_survey_relg1, q19_survey_relg2, q19_survey_relg3)

#nationalism---------------------------------------------------------------------
estimation_sample$national_index_skd_bw_5  =  kling_index_f3(.data = estimation_sample,
                                                             .impute = F,
                                                             ... = q20_survey_pancasila_h3, q21_survey_nation_ethnic_h3)

#corruption----------------------------------------------------------------------
estimation_sample$corruption_index_skd_bw_5  = kling_index_f3(.data = estimation_sample,
                                                              .impute = F,
                                                              ... = q16_survey_transparent_h2, q13_survey_success_reason_1_merit_h2, 
                                                              q13_survey_success_reason_3_sara, q13_survey_success_reason_2_connection_h2,
                                                              q14_survey_test_vs_connection)

#merge in
analysis_df_subset %<>%
  left_join(., estimation_sample_javanese %>% dplyr::select(ROWNUM_ID, java_index_skd_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample_non_javanese %>% dplyr::select(ROWNUM_ID, non_java_index_skd_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, regional_index_skd_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, religious_index_skd_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, national_index_skd_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, corruption_index_skd_bw_5), by = "ROWNUM_ID")


#----------------------------------------------------------------------------------
##kling indices for passing final exam (5pp)--------------------------------------------
#----------------------------------------------------------------------------------

#make various subsets ----------------------------------------------------------
estimation_sample_javanese = analysis_df_subset %>% filter(abs(forcing_final) < 5, java_indicator == 1)
estimation_sample_non_javanese = analysis_df_subset %>% filter(abs(forcing_final) < 5, java_indicator == 0)
estimation_sample = analysis_df_subset %>% filter(abs(forcing_final) < 5)

#javanese preferntialism ----------------------------------------------------------
estimation_sample_javanese$java_index_final_bw_5 = kling_index(.data = estimation_sample_javanese,
                                                               .impute = F,
                                                               ... = q17_survey_java1, q17_survey_java2)

estimation_sample_non_javanese$non_java_index_final_bw_5 = kling_index(.data = estimation_sample_non_javanese,
                                                                       .impute = F,
                                                                       ... = q17_survey_java1, q17_survey_java2)

#regional preferntialism ----------------------------------------------------------
estimation_sample$regional_index_final_bw_5 =  kling_index(.data = estimation_sample,
                                                           .impute = F,
                                                           ... = q18_survey_daerah1, q18_survey_daerah2, q18_survey_daerah3)

#religious intolerance ----------------------------------------------------------
estimation_sample$religious_index_final_bw_5 =  kling_index(.data = estimation_sample,
                                                            .impute = F,
                                                            ... = q19_survey_relg1, q19_survey_relg2, q19_survey_relg3)

#nationalism---------------------------------------------------------------------
estimation_sample$national_index_final_bw_5  =  kling_index(.data = estimation_sample,
                                                            .impute = F,
                                                            ... = q20_survey_pancasila_h3, q21_survey_nation_ethnic_h3)

#corruption----------------------------------------------------------------------
estimation_sample$corruption_index_final_bw_5  = kling_index(.data = estimation_sample,
                                                             .impute = F,
                                                             ... = q16_survey_transparent_h2, q13_survey_success_reason_1_merit_h2, 
                                                             q13_survey_success_reason_3_sara, q13_survey_success_reason_2_connection_h2,
                                                             q14_survey_test_vs_connection)


#merge in
analysis_df_subset %<>%
  left_join(., estimation_sample_javanese %>% dplyr::select(ROWNUM_ID, java_index_final_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample_non_javanese %>% dplyr::select(ROWNUM_ID, non_java_index_final_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, regional_index_final_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, religious_index_final_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, national_index_final_bw_5), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, corruption_index_final_bw_5), by = "ROWNUM_ID")


#----------------------------------------------------------------------------------
##kling indices for passing final exam (10pp)--------------------------------------------
#----------------------------------------------------------------------------------

#make various subsets ----------------------------------------------------------
estimation_sample_javanese = analysis_df_subset %>% filter(abs(forcing_final) < 10, java_indicator == 1)
estimation_sample_non_javanese = analysis_df_subset %>% filter(abs(forcing_final) < 10, java_indicator == 0)
estimation_sample = analysis_df_subset %>% filter(abs(forcing_final) < 10)

#javanese preferntialism ----------------------------------------------------------
estimation_sample_javanese$java_index_final_bw_10 = kling_index(.data = estimation_sample_javanese,
                                                               .impute = F,
                                                               ... = q17_survey_java1, q17_survey_java2)

estimation_sample_non_javanese$non_java_index_final_bw_10 = kling_index(.data = estimation_sample_non_javanese,
                                                                       .impute = F,
                                                                       ... = q17_survey_java1, q17_survey_java2)

#regional preferntialism ----------------------------------------------------------
estimation_sample$regional_index_final_bw_10 =  kling_index(.data = estimation_sample,
                                                           .impute = F,
                                                           ... = q18_survey_daerah1, q18_survey_daerah2, q18_survey_daerah3)

#religious intolerance ----------------------------------------------------------
estimation_sample$religious_index_final_bw_10 =  kling_index(.data = estimation_sample,
                                                            .impute = F,
                                                            ... = q19_survey_relg1, q19_survey_relg2, q19_survey_relg3)

#nationalism---------------------------------------------------------------------
estimation_sample$national_index_final_bw_10  =  kling_index(.data = estimation_sample,
                                                            .impute = F,
                                                            ... = q20_survey_pancasila_h3, q21_survey_nation_ethnic_h3)

#corruption----------------------------------------------------------------------
estimation_sample$corruption_index_final_bw_10  = kling_index(.data = estimation_sample,
                                                             .impute = F,
                                                             ... = q16_survey_transparent_h2, q13_survey_success_reason_1_merit_h2, 
                                                             q13_survey_success_reason_3_sara, q13_survey_success_reason_2_connection_h2,
                                                             q14_survey_test_vs_connection)


#merge in
analysis_df_subset %<>%
  left_join(., estimation_sample_javanese %>% dplyr::select(ROWNUM_ID, java_index_final_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample_non_javanese %>% dplyr::select(ROWNUM_ID, non_java_index_final_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, regional_index_final_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, religious_index_final_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, national_index_final_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, corruption_index_final_bw_10), by = "ROWNUM_ID")


#----------------------------------------------------------------------------------
##kling indices for point estimates (10pp) for SKD treatment--------------------------
#----------------------------------------------------------------------------------
#N.B. this particular exam is out of 500 points, which is why subsetting to 50 points is 10pp.

#make various subsets ----------------------------------------------------------
estimation_sample_javanese = analysis_df_subset %>% filter(abs(forcing_skd) < 50, java_indicator == 1)
estimation_sample_non_javanese = analysis_df_subset %>% filter(abs(forcing_skd) < 50, java_indicator == 0)
estimation_sample = analysis_df_subset %>% filter(abs(forcing_skd) < 50)


#javanese preferntialism ----------------------------------------------------------
estimation_sample_javanese$java_index_skd_bw_10 = kling_index_f3(.data = estimation_sample_javanese,
                                                                .impute = F,
                                                                ... = q17_survey_java1, q17_survey_java2)

estimation_sample_non_javanese$non_java_index_skd_bw_10 = kling_index_f3(.data = estimation_sample_non_javanese,
                                                                        .impute = F,
                                                                        ... = q17_survey_java1, q17_survey_java2,)

#regional preferntialism ----------------------------------------------------------
estimation_sample$regional_index_skd_bw_10 =  kling_index_f3(.data = estimation_sample,
                                                            .impute = F,
                                                            ... = q18_survey_daerah1, q18_survey_daerah2, q18_survey_daerah3)

#religious intolerance ----------------------------------------------------------
estimation_sample$religious_index_skd_bw_10 =  kling_index_f3(.data = estimation_sample,
                                                             .impute = F,
                                                             ... = q19_survey_relg1, q19_survey_relg2, q19_survey_relg3)

#nationalism---------------------------------------------------------------------
estimation_sample$national_index_skd_bw_10  =  kling_index_f3(.data = estimation_sample,
                                                             .impute = F,
                                                             ... = q20_survey_pancasila_h3, q21_survey_nation_ethnic_h3)

#corruption----------------------------------------------------------------------
estimation_sample$corruption_index_skd_bw_10  = kling_index_f3(.data = estimation_sample,
                                                              .impute = F,
                                                              ... = q16_survey_transparent_h2, q13_survey_success_reason_1_merit_h2, 
                                                              q13_survey_success_reason_3_sara, q13_survey_success_reason_2_connection_h2,
                                                              q14_survey_test_vs_connection)

#merge in
analysis_df_subset %<>%
  left_join(., estimation_sample_javanese %>% dplyr::select(ROWNUM_ID, java_index_skd_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample_non_javanese %>% dplyr::select(ROWNUM_ID, non_java_index_skd_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, regional_index_skd_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, religious_index_skd_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, national_index_skd_bw_10), by = "ROWNUM_ID") %>%
  left_join(., estimation_sample %>% dplyr::select(ROWNUM_ID, corruption_index_skd_bw_10), by = "ROWNUM_ID")

analysis_df_subset <- analysis_df_subset %>% mutate_all(~ifelse(is.nan(.), NA, .))


